package com.example.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Dao.FolderMapper;
import com.example.Entity.Dto.PageParmar;
import com.example.Entity.Dto.UserInfo;
import com.example.Entity.Po.tn_sys_folder;
import com.example.Entity.Vo.APIResult;
import com.example.IService.IFolderService;
import com.example.Utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FolderService extends BaseService implements IFolderService {

    @Resource
    FolderMapper folderMapper;

    /**
     * 获取用户的文件夹
     * @param pageParmar 分页
     * @return APIResult
     */
    @Override
    public APIResult GetUserFolder(PageParmar pageParmar) {
        if (pageParmar==null)
            return APIResult.error("请传入必要的参数");
        UserInfo userInfo = GetSessionInfo();
        QueryWrapper<tn_sys_folder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userInfo.getId());
        if (StringUtils.isNotBlank(pageParmar.getKeyword()))
            queryWrapper.like("foldername",pageParmar.getKeyword());
        queryWrapper.orderByDesc("createtime");
        IPage<tn_sys_folder> ipage = new Page<>(pageParmar.getPageNo(),pageParmar.getPageSize());
        IPage<tn_sys_folder> data = folderMapper.selectPage(ipage,queryWrapper);
        return APIResult.success("",data);
    }

    /**
     * 添加文件夹
     * @param name 文件夹名称
     * @return APIResult
     */
    @Override
    public APIResult AddFolder(String name) {
        if (StringUtils.isBlank(name))
            return APIResult.error("请传入文件夹名称");
        UserInfo userInfo = GetSessionInfo();
        QueryWrapper<tn_sys_folder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("foldername",name);
        queryWrapper.eq("userid",userInfo.getId());
        List<tn_sys_folder> b = folderMapper.selectList(queryWrapper);
        if (!b.isEmpty())
            return APIResult.error("不允许重复添加文件夹");
        tn_sys_folder folder = new tn_sys_folder();
        folder.setCreatetime(new Date());
        folder.setFoldername(name);
        folder.setUserid(userInfo.getId());
        folder.setUserame(userInfo.getUsername());
        folder.setUuid(GetUUID());
        int successcount = folderMapper.insert(folder);
        return successcount>0?APIResult.success():APIResult.error("添加失败");
    }

    /**
     * 获取用户所有的文件夹名
     * @return APIResult
     */
    @Override
    public APIResult GetFolderNameAll() {
        UserInfo userInfo = GetSessionInfo();
        QueryWrapper<tn_sys_folder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userInfo.getId());
        queryWrapper.orderByDesc("createtime");
        List<tn_sys_folder> b = folderMapper.selectList(queryWrapper);
        if (b.isEmpty())
            return APIResult.success();
        return APIResult.success("",b.stream().map(tn_sys_folder::getFoldername).collect(Collectors.toList()));
    }
}
